/**
 * 登录页面
 */
layui.define(['form', 'base'], function (exports) {

    var $ = layui.jquery, layer = layui.layer, $setting = layui.config, $http = layui.http, $global = layui.common,
        form = layui.form();
    //remove localStorage
    $global.fn.removeUserData();
    window.localStorage.removeItem('userInfoUrl');

    $('#verify_img').attr('src', $setting.getUri() + $setting.config.index.captcha + '?rd=' + Math.random());
    $('#verify_img').on('click', function () {
        $('#verify_img').attr('src', $setting.getUri() + $setting.config.index.captcha + '?rd=' + Math.random());
    });
    form.verify({
        pass: [
            /^[\S]{6,12}$/
            , '密码必须6到12位，且不能出现空格'
        ],
        verify: [/^[\S]{4}$/, '验证码输入错误']
    });

    form.on('submit(*)', function (data) {
        var formData = data.field;
        $http.post($setting.config.index.login, formData).send(function (obj) {
            if (obj.code != '0') {
                layer.msg(obj.message, { icon: 2 });
                $('#verify_img').attr('src', $setting.getUri() + $setting.config.index.captcha + '?rd=' + Math.random());
            } else {
                $global.fn.removeUserData();
                $global.fn.storeUserData(JSON.stringify(obj.data));
                window.localStorage.setItem('userInfoUrl', $setting.getUri() + $setting.config.user.info);
                window.location.href = $setting.getLocalUri() + '/index.html';
            }
        });
        return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
    });
    exports('../app/login', {});
});